<?php

require_once('class.phpmailer.php');

function sendEmail($piDestEmail, $piSenderEmail, $piEmailContent, $piEmailSubj, $piDestName, $piSenderName) {
	if ($piDestEmail != "" && $piEmailContent != "" && $piSenderEmail != "") {
		/*
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$headers .= 'From: WinningMktg' . "\r\n" .
			'Reply-To: ' . $piSenderEmail . "\r\n" .
			'X-Mailer: PHP/' . phpversion();
	
		// Send
		mail($piDestEmail, $piEmailSubj, $piEmailContent, $headers);
		*/
		$mail             = new PHPMailer(); // defaults to using php "mail()"
		$mail->IsSendmail(); // telling the class to use SendMail transport
		$body             = $piEmailContent;
		$body             = eregi_replace("[\]",'',$body);
		$mail->AddReplyTo($piSenderEmail,$piSenderName);
		$mail->SetFrom($piSenderEmail, $piSenderName);
		$address = $piDestEmail;
		$mail->AddAddress($address, $piDestName);
		$mail->Subject    = $piEmailSubj;
		$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
		$mail->MsgHTML($body);
		
		if(!$mail->Send()) {
			echo "Mailer Error: " . $mail->ErrorInfo;
		}
	}

}

function sendEnquiryEmail($piSenderName, $piSenderEmail, $piContactNumber, $piMessage, $piFacsimile) {

	$enquiryTargetEmail="mjtam79@gmail.com";
	
	if ($piSenderEmail != "" && $piMessage != "") {
		
		$tmpMessage = str_replace("\n", "<br/>", $piMessage);
		$tmpFacsimile = str_replace("\n", "<br/>", $piFacsimile);
		
		$emailSubject = "Enquiry/Feedback";
		$emailContent = "<table>" .
			"<tr><td align='right' valign='top' width='20%'>Enquirer Name:</td><td valign='top' width='80%'><nobr>" . $piSenderName . "</nobr></td></tr>" .
			"<tr><td align='right' valign='top'>Enquirer Email:</td><td valign='top'><nobr>" . $piSenderEmail . "</nobr></td></tr>" .
			"<tr><td align='right' valign='top'><nobr>Contact Number:</nobr><nobr></td><td valign='top'>" . $piContactNumber . "</nobr></td></tr>" .
			"<tr><td align='right' valign='top'>Message:</td><td valign='top'>" . $tmpMessage . "</td></tr>" .
			"<tr><td align='right' valign='top'>Facsimile:</td><td valign='top'>" . $tmpFacsimile . "</td></tr>" .
			"</table>";
	
		// Send
		sendEmail($enquiryTargetEmail, $piSenderEmail, $emailContent, $emailSubject, "Winning Mktg", $piSenderName);
	}

}

function getOrderDetailHtmlTable($piOrderHeader, $piOrderDetailArr) {
	$htmlTable = "<table border='1'>" .
		"<tr><th>Product Code</th><th>Product Name</th>";
	
	if ($piOrderHeader->getDeliveryMode() == "POST") {
		$htmlTable = $htmlTable . "<th>Shipping Available</th>";
		$tmpColspanForTotal = 8;
	}
	
	$htmlTable = $htmlTable . "<th>Size</th><th>Color</th><th>Quantity</th><th>Unit Price</th>";
	
	$tmpColspanForTotal = 6;
	if ($piOrderHeader->getDeliveryMode() == "POST") {
		$htmlTable = $htmlTable . "<th>Shipment Fee</th>";
		$tmpColspanForTotal = 8;
	}
	
	$htmlTable = $htmlTable . "<th>Sub-Total</th></tr>";
	
	for ($i=0; $i < sizeof($piOrderDetailArr); $i++) {
		$rowIDAttr = $i;
		$shipmentFee = 0;
		$orderDetail = $piOrderDetailArr[$i];
		$htmlTable = $htmlTable . "<tr><td>" . $orderDetail->getProductCode() . "</td>";
		$htmlTable = $htmlTable . "<td>" . $orderDetail->getProductName() . "</td>";
		if ($piOrderHeader->getDeliveryMode() == "POST") {
			$htmlTable = $htmlTable . "<td>" . $orderDetail->getPostAllow() . "</td>";
		}
		$htmlTable = $htmlTable . "<td>" . $orderDetail->getSizeSelected() . "</td>";
		$htmlTable = $htmlTable . "<td>" . $orderDetail->getColorSelected() . "</td>";
		$htmlTable = $htmlTable . "<td align='right'>" . $orderDetail->getQuantity() . "</td>";
		$htmlTable = $htmlTable . "<td align='right'>" . $orderDetail->getUnitPrice() . "</td>";
		
		if ($piOrderHeader->getDeliveryMode() == "POST") {
			if ($orderDetail->getPostAllow() == "Y") {
				$shipmentFee = $orderDetail->getPostage();
			}
			$htmlTable = $htmlTable . "<td align='right'>" . number_format($shipmentFee * $orderDetail->getQuantity(), 2, '.', '') . "</td>";
		}
		
		$subTotal = $orderDetail->getQuantity() * $orderDetail->getUnitPrice() + $shipmentFee;
		
		$htmlTable = $htmlTable . "<td align='right'>" . number_format($subTotal, 2, '.', '') . "</td></tr>";
	}
	$htmlTable = $htmlTable . "<tr><td colspan='" . $tmpColspanForTotal . "' align='right'><b>Total (S$):</b>&nbsp;&nbsp;</td><td align='right'><b>" . $piOrderHeader->getTotalAmt() . "</b></td></tr>" .
	"</table>";
	
	return $htmlTable;
}

function sendOrderConfirmation($piOrderHeader, $piOrderDetailArr) {
	$orderNotificationEmail="mjtam79@gmail.com";
	
	$emailSubject = "WinningMktg Order Confirmation (Order Number: " . str_pad($piOrderHeader->getOrderId(), 8, "0", STR_PAD_LEFT) . ")";
	$orderDetailTable = getOrderDetailHtmlTable($piOrderHeader, $piOrderDetailArr);
	$emailContent = "<table>" .
		"<tr><td valign='top' colspan='2'>Dear " . $piOrderHeader->getCustName() . ",</td></tr>" .
		"<tr><td valign='top' colspan='2'>Thank you for ordering from www.winningmktg.com!</td></tr>" .
		"<tr><td valign='top' colspan='2'>Your order is confirmed on " . $piOrderHeader->getOrderDt() . ".</td></tr>" .
		"<tr><td valign='top' colspan='2'>Below is the order detail for your reference:</td></tr>" .
		"<tr><td colspan='2'>&nbsp;</td></tr>" .
		"<tr><td align='right' valign='top' width='20%'><b>Order Number:</b></td><td valign='top' width='80%'><nobr>" . str_pad($piOrderHeader->getOrderId(), 8, "0", STR_PAD_LEFT) . "</nobr></td></tr>" .
		"<tr><td align='right' valign='top' width='20%'><b>Customer Name:</b></td><td valign='top' width='80%'><nobr>" . $piOrderHeader->getCustName() . "</nobr></td></tr>" .
		"<tr><td align='right' valign='top'><b>Mailing Address:</b></td><td valign='top'><nobr>" . str_replace("\n", "<br/>", $piOrderHeader->getCustAddress()) . "</nobr></td></tr>" .
		"<tr><td align='right' valign='top'><nobr><b>Contact Number:</b></nobr><nobr></td><td valign='top'>" . $piOrderHeader->getCustContact() . "</nobr></td></tr>" .
		"<tr><td align='right' valign='top'><b>Email:</b></td><td valign='top'><nobr>" . $piOrderHeader->getCustEmail() . "</nobr></td></tr>" .
		"<tr><td align='right' valign='top'><b>Delivery Mode:</b></td><td valign='top'>" . $piOrderHeader->getDeliveryMode() . "</td></tr>" .
		"<tr><td align='right' valign='top'><b>Payment Mode:</b></td><td valign='top'>" . $piOrderHeader->getPaymentMode() . "</td></tr>" .
		"<tr><td align='right' valign='top'><b>Remark:</b></td><td valign='top'>" . str_replace("\n", "<br/>", $piOrderHeader->getRemark()) . "</td></tr>" .
		"<tr><td align='right' valign='top'><b>Order Date:</b></td><td valign='top'>" . $piOrderHeader->getOrderDt() . "</td></tr>" .
		"<tr><td colspan='2'>&nbsp;</td></tr>" .
		"<tr><td colspan='2'><b><i>Order Detail:</i></b></td></tr>" .
		"<tr><td colspan='2'>" . $orderDetailTable . "</td></tr>" . 
		"<tr><td colspan='2'>&nbsp;</td></tr>";
	
	if ($piOrderHeader->getPaymentMode() == "OFFLINE") {
		$emailContent = $emailContent . "</tr><tr><td colspan='2'><b><i>Payment Instruction:</i></b></td></tr>" .
		"<tr><td valign='top' colspan='2'>You have selected Offline Bank Transfer as Payment Mode.<br/>" .
		"Please transfer the payment to OCBC Account 511-048761-001 within 3 working days <br/>and reply this e-mail to notify Winning Marketiing.</td></tr>";
	}
	
	$emailContent = $emailContent . "<tr><td colspan='2'>&nbsp;</td></tr><tr><td colspan='2'><b><i>Goods Collection:</i></b></td></tr>" .
		"<tr><td valign='top' colspan='2'>Please visit any Winning Marketing outlet for self collect goods 3 working days after payment.<br/>" .
		"The list of outlets is available in Winning Marketing web site: http://www.winningmktg.com.</td></tr>";
	
	$emailContent = $emailContent . "<tr><td colspan='2'>&nbsp;</td></tr><tr><td valign='top' colspan='2'>Should you have any query about this order please contact Winning Marketing:</td></tr>" .
		"<tr><td align='right' valign='top'>E-mail:</td><td>" . $orderNotificationEmail . "</td></tr>" .
		"<tr><td align='right' valign='top'>Contact Number:</td><td>+65 6481 3393</td></tr>" .
		"<tr><td colspan='2'>&nbsp;</td></tr>" .
		"<tr><td valign='top' colspan='2'>Thank you for shopping with us.</td></tr>" .
		"</table>";

	// Send Order Confrimation to customer
	sendEmail($piOrderHeader->getCustEmail(), $orderNotificationEmail, $emailContent, $emailSubject, $piOrderHeader->getCustName(), "Winning Mktg");
	
	// Send Order Notification to WinningMktg 
	sendOrderNotification($piOrderHeader, $piOrderDetailArr, $orderDetailTable);
}

function sendOrderNotification($piOrderHeader, $piOrderDetailArr, $orderDetailTable) {
	$orderNotificationEmail="mjtam79@gmail.com";
	
	$emailSubject = "WinningMktg Order Notification (Order Number: " . str_pad($piOrderHeader->getOrderId(), 8, "0", STR_PAD_LEFT) . ")";
	$emailContent = "<table>" .
		"<tr><td valign='top' colspan='2'>Dear Winning Marketing Admin,</td></tr>" .
		"<tr><td valign='top' colspan='2'>There is an online order received!</td></tr>" .
		"<tr><td valign='top' colspan='2'>Below is the order detail for your reference:</td></tr>" .
		"<tr><td colspan='2'>&nbsp;</td></tr>" .
		"<tr><td align='right' valign='top' width='20%'><b>Order Number:</b></td><td valign='top' width='80%'><nobr>" . str_pad($piOrderHeader->getOrderId(), 8, "0", STR_PAD_LEFT) . "</nobr></td></tr>" .
		"<tr><td align='right' valign='top' width='20%'><b>Customer Name:</b></td><td valign='top' width='80%'><nobr>" . $piOrderHeader->getCustName() . "</nobr></td></tr>" .
		"<tr><td align='right' valign='top'><b>Mailing Address:</b></td><td valign='top'><nobr>" . str_replace("\n", "<br/>", $piOrderHeader->getCustAddress()) . "</nobr></td></tr>" .
		"<tr><td align='right' valign='top'><nobr><b>Contact Number:</b></nobr><nobr></td><td valign='top'>" . $piOrderHeader->getCustContact() . "</nobr></td></tr>" .
		"<tr><td align='right' valign='top'><b>Email:</b></td><td valign='top'><nobr>" . $piOrderHeader->getCustEmail() . "</nobr></td></tr>" .
		"<tr><td align='right' valign='top'><b>Delivery Mode:</b></td><td valign='top'>" . $piOrderHeader->getDeliveryMode() . "</td></tr>" .
		"<tr><td align='right' valign='top'><b>Payment Mode:</b></td><td valign='top'>" . $piOrderHeader->getPaymentMode() . "</td></tr>" .
		"<tr><td align='right' valign='top'><b>Remark:</b></td><td valign='top'>" . str_replace("\n", "<br/>", $piOrderHeader->getRemark()) . "</td></tr>" .
		"<tr><td align='right' valign='top'><b>Order Date:</b></td><td valign='top'>" . $piOrderHeader->getOrderDt() . "</td></tr>" .
		"<tr><td colspan='2'>&nbsp;</td></tr>" .
		"<tr><td colspan='2'><b><i>Order Detail:</i></b></td></tr>" .
		"<tr><td colspan='2'>" . $orderDetailTable . "</td></tr>" .
		"<tr><td colspan='2'>&nbsp;</td></tr>" .
		"<tr><td valign='top' colspan='2'>For payment status, please enquiry from PayPal.</td></tr>" .
		"</table>";

	// Send Order Notification to WinningMktg
	sendEmail($orderNotificationEmail, $piOrderHeader->getCustEmail(), $emailContent, $emailSubject, "Winning Mktg", $piOrderHeader->getCustName());
}
?>